package com.voxel.sense.infomsg.module.user.search;

import android.text.TextUtils;

import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.ListUtil;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.utils.ToastUtil;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.base.RecyclerViewPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.constants.EventBusConstants;
import cyy.example.com.facotry.model.EventEntity;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.response.StaffEntity;
import cyy.example.com.facotry.net.helper.NetHelper;

/**
 * 用户搜索的P层
 * Created by ChenYiYao on 2018/3/7.
 */

public class SearchPresenter extends RecyclerViewPresenter<SearchUserContract.View>
        implements SearchUserContract.Presenter, DataSource.Callback<BaseEntity<StaffEntity>> {
    private String mSearchKey = "";
    private String mUserStatusType;



    public SearchPresenter(SearchUserContract.View view, String userStatusType) {
        super(view);
        this.mUserStatusType = userStatusType;
    }

    @Override
    public void start() {
        super.start();
        EventBus.getDefault().register(this);
    }

    @Override
    public void destroy() {
        super.destroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void loadData() {
        netLoadData(getPAGE_NUMB());
    }

    @Override
    public void loadMoreData() {
        netLoadData(getPAGE_NUMB() );
    }

    @Override
    public void doSearch(String condition) {
        if (NetworkUtils.isConnected()) {
            mSearchKey = condition;
            startRefreshData();
        } else {
            ToastUtil.showToastLong(Factory.app(), Factory.app().getString(R.string.network_unavailable));
            mView.onRefreshFinish();
            mView.showEmptyView(Factory.app().getString(R.string.network_unavailable), true);
        }
    }

    @Override
    public void doLoadMoreData() {
        if (NetworkUtils.isConnected()) {
            startLoadMoreData();
        } else {
            ToastUtil.showToastLong(Factory.app(), Factory.app().getString(R.string.network_unavailable));
            mView.onLoadMoreFinish();
        }

    }

    private void netLoadData(int PAGE_NUMB) {
        NetHelper.getStaffList(PAGE_NUMB, getPageDataSize(),mSearchKey, null,null,mUserStatusType,this);
    }

    @Override
    public void onDataLoaded(BaseEntity<StaffEntity> data, int reqId) {
        if (mView == null)
            return;
        mView.dimissLoading();
        if (data.isSuccess()) {
            //加载成功，记录页数
            StaffEntity bean = data.getData();
            List<StaffEntity.SysUserListBean> sysUserList = bean.getSysUserList();
            if (sysUserList != null) {
                loadDataComplete(sysUserList);
            } else {
                LoadDataFailure();
            }
        } else {
            LoadDataFailure();
        }
    }

    @Override
    public void onDataNotAvailable(String strRes, int reqId) {
        if (mView == null)
            return;
        LoadDataFailure();
        mView.showEmptyView(strRes, true);
    }

    @Subscribe
    public void onEventBus(EventEntity entity) {
        if (mView == null)
            return;
        if (entity != null && entity.key.equals(EventBusConstants.SEARCH_STAFF)) {
            //收到搜索的消息通知
            String mName = (String) entity.value;
            //拉起刷新ui事件
            mView.onUiRefresh(mName);
        }else if (Constants.INFOMARL_USER_YPE.equals(mUserStatusType) && EventBusConstants.CHECK_USER_SUCCESS.equals(entity.key)) {
            //审核类型列表，且是审核通知
            RecyclerAdapter mAdapter = mView.getRecyclerViewAdapter();
            String value = (String) entity.value;
            if (!TextUtils.isEmpty(value) && mAdapter != null && !ListUtil.isEmpty(mAdapter.getItems())) {
                List<StaffEntity.SysUserListBean> items = mAdapter.getItems();
                for (int i = items.size() - 1; i >= 0; i--) {
                    if (value.equals(items.get(i).getUserId())) {
                        items.remove(i);
                        mAdapter.notifyDataSetChanged();
                        return;
                    }
                }
            }
        }
    }

}
